package geometry2D;

import byggeTegner.DrawPanel;
import context.*;


import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;


import org.json.JSONArray;
import org.json.JSONException;

import util.ValueSpinner;
import util.ValueType;

// ********************************************************************
// *** CLASS: UTM32Location
// ********************************************************************
public class UTM32Location extends ValueType implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
  static final String TAG="geometry2D.UTM32Location";

  private double EAST;  // x-coordinate in meter from ??? (London ?)
  private double NORTH; // y-coordinate in meter from ??? (Ekvator ?)

  public UTM32Location(double east,double north)
  { this.EAST=east; this.NORTH=north; }
  
  public UTM32Location(JSONArray json)  throws JSONException
  { this(json.getDouble(0),json.getDouble(1)); }
  
  public JSONArray encode() throws JSONException
  {	JSONArray obj=new JSONArray();
	obj.put(getEast());
	obj.put(getNorth());
	return(obj);
  }
  
  public UTM32Location copy()
  { return(new UTM32Location(EAST,NORTH)); }

  public double getEast() { return(EAST); }
  public double getNorth() { return(NORTH); }
  public void setEast(double east) { if(east!=this.EAST) { this.EAST=east; FIRE_CHANGE_EVENT("EAST"); } }
  public void setNorth(double north) { if(north!=this.NORTH) { this.NORTH=north; FIRE_CHANGE_EVENT("NORTH"); } }
  
  public MetricPoint2D toMetricPoint2D(DrawPanel drawPanel)
  { Grid grid=drawPanel.getObjectModel().getGrid();
	double x=EAST-grid.getOrigoEast();
	double y=grid.getOrigoNorth()-NORTH;
	return(new MetricPoint2D(x,y));
  }
  
//  public PixelPoint toPixelPoint(UTM32Grid grid)
//  { double x=toPixelX(grid);
//    double y=toPixelY(grid);
//    return(new PixelPoint(x,y));
//  }
//  private double toPixelX(UTM32Grid grid)
//  { double xPix=grid.xMetersToPixel()*(EAST-grid.getOrigoEast());
//    xPix=Util2.round(xPix,100);
//    return(grid.getOrigoPixelX()+xPix);
//  }
//  private double toPixelY(UTM32Grid grid)
//  { double yPix=grid.yMetersToPixel()*(grid.getOrigoNorth()-NORTH);
//    yPix=Util2.round(yPix,100);
//    return(grid.getOrigoPixelY()+yPix);
//  }

//  // ******************************************************************
//  // *** METHOD: getDistanceInMeter
//  // ******************************************************************
//  public double getDistanceInMeter(UTM32Location other)
//  { Point2D p1=new Point2D.Double(this.getEast(),this.getNorth());
//    Point2D p2=new Point2D.Double(other.getEast(),other.getNorth());
//    return(p1.distance(p2));
//  }

  // ****************************************************************
  // *** METHOD: getSpinnerPanel
  // ****************************************************************
  public JPanel getSpinnerPanel()
  { final double MINIMUM=0;
    final double MAXIMUM=Integer.MAX_VALUE;
    JPanel panel=new JPanel();
    panel.setLayout(new GridLayout(2,2));
    double stepSize=0.01; // 1 cm
    panel.add(new JLabel("  X:   UTM32 EAST"));
    panel.add(new ValueSpinner(getEast(),MINIMUM,MAXIMUM,stepSize)
    { static final long serialVersionUID=0; // AD'HOC
      public void update(double value) { setEast(value); }
    });
    panel.add(new JLabel("  Y:   UTM32 NORTH"));
    panel.add(new ValueSpinner(getNorth(),MINIMUM,MAXIMUM,stepSize)
    { static final long serialVersionUID=0; // AD'HOC
      public void update(double value) { setNorth(value); }
    });
    return(panel);
  } 

  // ******************************************************************
  // *** METHOD: toString
  // ******************************************************************
  public String toString()
  { return("UTM32Location[east="+getEast()+",north="+getNorth()+"]"); }
}
